package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.InspectionStandardBean;
import com.shqcjd.sinopecdemomobile.ui.adapter.InspectionStandardAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.UIUtils;
import com.shqcjd.sinopecdemomobile.widget.FillListview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class InspectionStandardActivity extends BaseActivity {
    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.lv_inspection_standard)
    FillListview lvInspectionStandard;
    @BindView(R.id.tv_inspection_standard_name)
    TextView tvInspectionStandardName;
    @BindView(R.id.tv_inspection_standard_kks)
    TextView tvInspectionStandardKks;
    private Context context;
    private InspectionStandardAdapter inspectionStandardAdapter;
    private List<String> mList = new ArrayList<>();
    private int eId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_inspection_standard;
    }

    @Override
    public void init() {
        super.init();
        eId = getIntent().getIntExtra("eId", 1);
    }

    @Override
    public void initView() {
        super.initView();
        context = this;
        tvIncludeTitle.setText("巡检标准");
        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getData();
    }

    private void getData() {
        ApiService.inspectionStandardHttp(eId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<InspectionStandardBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<InspectionStandardBean> responseData) {
                        if(responseData.retCode!=10000){
                            return;
                        }
                        if(inspectionStandardAdapter==null){
                            inspectionStandardAdapter = new InspectionStandardAdapter(responseData.data.items, context);
                            lvInspectionStandard.setAdapter(inspectionStandardAdapter);
                            UIUtils.setListViewHeightBasedOnChildren(lvInspectionStandard);
                            tvIncludeFinish.requestFocus();
                        }

                        tvInspectionStandardName.setText(responseData.data.equipment_name);
                        tvInspectionStandardKks.setText("KKS: "+responseData.data.kks);
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

}
